<?php
require('includes/application_top.php');


$action = (isset($_GET['action']) ?$_GET['action'] : '');

 $_POST['from']=(int)$_GET['orders_id'];
 $_POST['to']=(int)$_GET['orders_id'];

if (zen_not_null($action)) {
switch ($action) {
case 'Exportorder':
if(isset($_POST['from']) &&is_numeric($_POST['from']) &&isset($_POST['to']) &&is_numeric($_POST['to']) &&$_POST['to']>=$_POST['from']   &&$_POST['to']!=''&&$_POST['from']!=''){

global $db;
$orders=TABLE_ORDERS;
$orders_status_history=TABLE_ORDERS_STATUS_HISTORY;
$orders_products=TABLE_ORDERS_PRODUCTS;
$products=TABLE_PRODUCTS;
$orders_products_attributes=TABLE_ORDERS_PRODUCTS_ATTRIBUTES;
$firstsql='';
$firstsql='select o.orders_id,o.customers_telephone,o.customers_email_address,o.customers_name,o.delivery_name,o.delivery_company,o.delivery_street_address,o.delivery_city,o.delivery_postcode,o.delivery_state,o.delivery_country,o.payment_method,o.shipping_method,o.date_purchased,o.coupon_code,o.currency,o.currency_value,o.order_total,o.ip_address,o.orders_status,(select comments from '.$orders_status_history .' where orders_id=o.orders_id order by orders_status_history_id desc limit 1) comments from '.$orders .' o  where  o.orders_id>='.$_POST['from'] .' and o.orders_id<='.$_POST['to'] .' order by o.date_purchased asc';
$resultone=$db->Execute($firstsql);
if($resultone->RecordCount()){
$date_purchased=date('Y-m-d', strtotime($resultone->fields['date_purchased']));
$fileName=str_replace('www.','',$_SERVER['SERVER_NAME']).'-'.$date_purchased.'-'.$_POST['from'];
require_once 'PHPWord.php'; 
// New Word Document
// New portrait section
$PHPWord = new PHPWord();

$orders_status_id=$resultone->fields['orders_status'];

$orders_status_value = $db->Execute("select orders_status_id, orders_status_name
                                 from " . TABLE_ORDERS_STATUS . "
                                 where orders_status_id='".$orders_status_id."' and language_id = '" . (int)$_SESSION['languages_id'] . "' order by orders_status_id limit 1");
								 							 					 
if($orders_status_value->RecordCount()){
 $orders_status_name=$orders_status_value->fields['orders_status_name'];

 }

$section = $PHPWord->createSection();
$tongdaonumber=$resultone->fields['comments'];
$tongdaonumber=substr(strrchr($tongdaonumber, ':'), 1);


$section->addText('订单号：'.ORDER_PREFIX. $_POST['from']. ' - '.$tongdaonumber);
$section->addText('订单状态：'.$orders_status_name);
$section->addTextBreak(1);
$center = array('spaceAfter' => 0, 'align' => 'center');
// Define table style arrays
$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80,'valign' =>  'center');
$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF','align' => 'center','alignMent' => 'right');

// Define cell style arrays
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');
// Add table style
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);

// Add table
$table = $section->addTable('myOwnTableStyle');

// Add row
$table->addRow(50);

while(!$resultone->EOF){ 

// Add cells
$table->addCell(5000, $styleCell)->addText('图片', $fontStyle);
$table->addCell(3000, $styleCell)->addText('型号&尺码', $fontStyle);
$table->addCell(1000, $styleCell)->addText('数量', $fontStyle);
$sqlsqltwo='';
$sqlsqltwo='select orders_products_id,op.orders_id,products_id,products_name,products_price,final_price,products_quantity, oss.currency_value  ,(select products_image from '.$products .' where products_id=op.products_id )  products_image   from '.$orders_products .' op inner join '.$orders .' oss on oss.orders_id=op.orders_id and op.orders_id='.$resultone->fields['orders_id'];
$sqltwo=$db->Execute($sqlsqltwo);
$total=0;
if($sqltwo->RecordCount()){
while (!$sqltwo->EOF){
$sql_value='';
$sql_value = "SELECT products_options_values
								FROM ".$orders_products_attributes ."
								WHERE orders_id ='".$resultone->fields['orders_id'] ."'
								AND orders_products_id =".$sqltwo->fields['orders_products_id'];
$r_value=array();
$op_value = $db->Execute($sql_value);
if($op_value->RecordCount()){
while (!$op_value->EOF){
array_push ( $r_value,$op_value->fields['products_options_values'] );
$op_value->MoveNext();
}
$products_attr='';
$products_attr ='  尺码: '. implode ( "--",$r_value );
}else{
$products_attr='';
$products_attr ='';
}

 $total+=$sqltwo->fields['final_price']*$sqltwo->fields['products_quantity'];

// Add more rows / cells


	$table->addRow();
	$cell = $table->addCell(5000);
	
	$img_paths='../images/'.$sqltwo->fields['products_image'];
	$img_width=getsize_imgw($img_paths);
 	$img_height=getsize_imgh($img_paths);
	
	$cell->addImage($img_paths,array('width'=>$img_width, 'height'=>$img_height, 'align'=>'center'));
	
	//$table->addCell(2000)->addImage('_earth.JPG', array('width'=>210, 'height'=>210, 'align'=>'center')));
	
	$table->addCell(3000)->addText($sqltwo->fields['products_name'].'  '.$products_attr);
	
	
	$table->addCell(1000)->addText($sqltwo->fields['products_quantity']);



$sqltwo->MoveNext();
}

$section->addTextBreak(1);
$section->addText('姓名: '.$resultone->fields['delivery_name']); 
$section->addText('国家: '.$resultone->fields['delivery_country']);
$section->addText('客人地址: '); 
$section->addText($resultone->fields['delivery_street_address']); 
$section->addText($resultone->fields['delivery_city'].','.$resultone->fields['delivery_state'].' '.$resultone->fields['delivery_postcode']); 
$section->addText($resultone->fields['delivery_country'].' ');
$section->addText('邮编: '.$resultone->fields['delivery_postcode']);
$section->addText('电话: '.$resultone->fields['customers_telephone']);


$section->addTextBreak(1);

$resultone->MoveNext();
}



}

// Save File
/*$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($fileName.'.docx');*/


$fileName = $fileName;
header("Content-type: application/vnd.ms-word");
header("Content-Disposition:attachment;filename=".$fileName.".docx");
header('Cache-Control: max-age=0');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');

}
}
}
}



function getsize_imgw($img_path){
     $size_src=getimagesize($img_path);
     $w=$size_src['0'];
     $h=$size_src['1'];
     //指定缩放出来的最大的宽度（也有可能是高度）
     $max=300;
     //根据最大值为300，算出另一个边的长度，得到缩放后的图片宽度和高度
     if($w > $h){
         $w=$max;
         $h=$h*($max/$size_src['0']);
     }else{
         $h=$max;
         $w=$w*($max/$size_src['1']);
     }
	 return $w;
	 }
	 
	 function getsize_imgh($img_path){
     $size_src=getimagesize($img_path);
     $w=$size_src['0'];
     $h=$size_src['1'];
     //指定缩放出来的最大的宽度（也有可能是高度）
     $max=300;
     //根据最大值为300，算出另一个边的长度，得到缩放后的图片宽度和高度
     if($w > $h){
         $w=$max;
         $h=$h*($max/$size_src['0']);
     }else{
         $h=$max;
         $w=$w*($max/$size_src['1']);
     }
	 return $h;
	 }
	 
	 
	 
?>